package com.tanhua.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.impl.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@PathVariable String id,@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
         PageResult pr = videosService.findComments(id,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 视频发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity sendComments(@PathVariable("id") String ids, @RequestBody Map map) {

        String comment = (String) map.get("comment");
        videosService.sendComments(ids, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户点赞
     */
    @PostMapping("/{uid}/like")
    public ResponseEntity saveFocus(@PathVariable("uid") String id) {
        videosService.savelike(id);
        return ResponseEntity.ok(null);

    }

    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    @CacheEvict(value = "videos", allEntries = true)  //清空缓存
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    @Cacheable(
            value = "videos",
            key = "T(com.tanhua.interceptor.UserHolder).getUserId()+'_'+#page+'_'+#pagesize")  //userid _ page_pagesize
    public PageResult queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return result;
    }
}